#!/bin/bash

set -e

echo "🐳 Minidis Docker 测试脚本"
echo "=========================="

# 检查 Docker 是否可用
if ! command -v docker &> /dev/null; then
    echo "❌ Docker 不可用，请先安装 Docker"
    exit 1
fi

echo "📦 构建本地 Docker 镜像..."

# 使用更简单的 Dockerfile，避免网络问题
cat > Dockerfile.local << 'EOF'
FROM rust:1.75

WORKDIR /app
COPY . .

RUN cargo build --release

EXPOSE 6379

CMD ["./target/release/minidis"]
EOF

echo "🔨 构建镜像..."
docker build -t minidis:local -f Dockerfile.local .

echo "🧹 清理临时文件..."
rm -f Dockerfile.local

echo "🚀 启动容器..."
docker run -d --name minidis-test -p 6380:6379 minidis:local

echo "⏳ 等待服务启动..."
sleep 3

echo "🧪 测试服务..."
if echo -e '*1\r\n$4\r\nPING\r\n' | nc 127.0.0.1 6380; then
    echo "✅ 测试成功！"
else
    echo "❌ 测试失败！"
fi

echo "🛑 停止并清理容器..."
docker stop minidis-test
docker rm minidis-test

echo "🎉 Docker 测试完成！"